package com.qk.controller;

import com.qk.common.Result;
import com.qk.entity.PageResult;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/roles")
    public Result list(String name, String label,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<Role> pageResult = roleService.list(name, label, page, pageSize);
        log.info("列表查询" +pageResult);
        return Result.success(roleService.list(name, label, page, pageSize));

    }

    @PostMapping("roles")
    public Result add(@RequestBody Role role) {
        log.info("添加" + role);
        roleService.add(role);
        return Result.success();
    }

    @DeleteMapping("/roles/{id}")
    public Result delete(@PathVariable Integer id) {
        roleService.delete(id);
        return Result.success();
    }
    @GetMapping("/roles/{id}")
    public Result getByID(@PathVariable Integer id) {
        return Result.success(roleService.getByID(id));
    }
    @PutMapping("/roles")
    public Result update(@RequestBody Role role) {
        roleService.update(role);
        return Result.success();
    }
    @GetMapping("/roles/list")
    public Result list() {
        return Result.success( roleService.getAll());
    }
}
